package com.shanghaionstar.remote.share.presenters;

import android.content.Context;

import com.onstar.cn.common.exception.OnStarException;
import com.onstar.cn.framework.mvp.base.IMVPScheduler;
import com.onstar.cn.framework.mvp.base.IMVPView;
import com.onstar.cn.framework.mvp.base.core.BasePresenter;
import com.onstar.cn.smart.BaseResponse;
import com.onstar.cn.vehicle.response.AuthorizedSubscriberResponse;
import com.shanghaionstar.remote.share.ShareRemoteService;
import com.shanghaionstar.remote.share.views.IShareRemoteListView;

import rx.functions.Action1;

/**
 * Created by 6335yz on 2016/6/25.
 */
public class AuthorizeEditPresenter extends BasePresenter  {
    private ShareRemoteService remoteService;
    private Context context;
    private IShareRemoteListView shareRemoteListView;

    public AuthorizeEditPresenter(IMVPScheduler mvpScheduler, Context context) {
        super(mvpScheduler);
        this.context = context;
        remoteService = new ShareRemoteService(context);
    }

    @Override
    public void setView(IMVPView view) {
        super.setView(view);
        shareRemoteListView = (IShareRemoteListView) view;
    }

    public void setAuthorizedStatus(AuthorizedSubscriberResponse request) {
        shareRemoteListView.setLoading(true);
        subscription = remoteService.putSharingAuthorization(request).subscribeOn(mvpScheduler.backgroundThread()).observeOn(mvpScheduler.mainThread())
                .subscribe(new Action1<BaseResponse>() {
                    @Override
                    public void call(BaseResponse response) {
                        shareRemoteListView.setLoading(false);
                        if(response.code.equalsIgnoreCase("E0000")) {
                            shareRemoteListView.onReponseSuccess(response);
                        }else{
                            shareRemoteListView.onRemoteShareSetFailed(null);
                        }
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        shareRemoteListView.setLoading(false);
                        if (throwable != null)
                            shareRemoteListView.onRemoteShareSetFailed((OnStarException) throwable);
                    }
                });
    }


}
